package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.util.DBHelper;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.wb.swt.SWTResourceManager;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Dialog_RegMember extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_id;
	private Text text_name;
	private Text text_btime;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_RegMember(Shell parent, int style) {
		super(parent, style);
		setText("会员注册：");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		shell.setSize(470, 352);
		shell.setText(getText());
		ShoppingUtil.centerWindows(shell);
		
		Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label.setBounds(55, 53, 95, 25);
		label.setText("会员号码：");
		
		text_id = new Text(shell, SWT.BORDER);
		text_id.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		text_id.setBounds(156, 53, 204, 31);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("姓名：");
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_1.setBounds(73, 114, 57, 25);
		
		text_name = new Text(shell, SWT.BORDER);
		text_name.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		text_name.setBounds(156, 114, 204, 31);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setText("生日：");
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		label_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_2.setBounds(73, 180, 57, 25);
		
		text_btime = new Text(shell, SWT.BORDER);
		text_btime.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		text_btime.setBounds(156, 177, 204, 31);
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String sql = "insert into tb_member values(?,?,?,?,?)";
				String mid = text_id.getText().trim();
				String name=text_name.getText().trim();
				String birthday = text_btime.getText().trim();
				try {
					int count=DBHelper.update(sql,mid,name,ShoppingUtil.toDate(birthday),0,1);
					if(count>0) {
						ShoppingUtil.showMessage(shell, "提示", "注册成功");
						shell.dispose();
					}else {
						ShoppingUtil.showMessage(shell, "提示", "注册失败");
						return;
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				} 
				
			}
		});
		button.setBounds(173, 246, 80, 27);
		button.setText("注册");
	}
	
}
